package cl.android.mochileros;

import android.app.Fragment;
import android.app.FragmentManager;
import android.graphics.Point;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.Projection;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapaFragment extends Fragment {
	private MapView mMapView;
	private GoogleMap mMap;
	private Bundle mBundle;
	private ImageButton btnAgregar;
	LatLng ultimoMarker;
	LatLng defaultPosition;
	
	public MapaFragment() {
		// TODO Auto-generated constructor stub
	}
	
	

	public MapaFragment(LatLng defaultPosition) {
		super();
		this.defaultPosition = defaultPosition;
	}



	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View inflatedView = inflater.inflate(R.layout.map_fragment_layout, container, false);

		MapsInitializer.initialize(getActivity());

		mMapView = (MapView) inflatedView.findViewById(R.id.map);
		mMapView.onCreate(mBundle);
		setUpMapIfNeeded(inflatedView);
		btnAgregar = (ImageButton) inflatedView.findViewById(R.id.agregar_lugar_btn);
		btnAgregar.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				onClickAgregar();
			}
		});
		if(defaultPosition != null){
			mMap.clear();
			mMap.addMarker(new MarkerOptions().position(defaultPosition).title("Nuevo Lugar"));
			CameraPosition position = new CameraPosition(defaultPosition, 10, 70, 1);
			CameraUpdate update = CameraUpdateFactory.newCameraPosition(position);
			mMap.animateCamera(update);
		}
		mMap.setOnMapLongClickListener(new OnMapLongClickListener() {
			
			@Override
			public void onMapLongClick(LatLng arg0) {
				mMap.clear();
//				Projection proj = mMap.getProjection();
//				Point coord = proj.toScreenLocation(arg0);
				LatLng nvaImperial = new LatLng(arg0.latitude, arg0.longitude);
				
				mMap.addMarker(new MarkerOptions().position(nvaImperial).title("Nuevo Lugar"));
				ultimoMarker = nvaImperial;
				
			}
		});
		return inflatedView;
	}
	
	public void onClickAgregar(){
		cargarFragment(new AgregarLugarFragment(ultimoMarker));
		if(ultimoMarker == null){
			cargarFragment(new AgregarLugarFragment());
		}else{
			cargarFragment(new AgregarLugarFragment(ultimoMarker));
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mBundle = savedInstanceState;
	}
	
	 public void cargarFragment(Fragment fragment){
			FragmentManager manager = getFragmentManager();
			manager.beginTransaction().add(R.id.mainContent, fragment, "fragmento").addToBackStack(null).commit();
		}

	private void setUpMapIfNeeded(View inflatedView) {
		if (mMap == null) {
			mMap = ((MapView) inflatedView.findViewById(R.id.map)).getMap();
			if (mMap != null) {
				setUpMap();
			}
		}
	}

	private void setUpMap() {
		CameraPosition position = new CameraPosition(new LatLng(-38.6737, -72.4474), 6, 70, 1);
		CameraUpdate update = CameraUpdateFactory.newCameraPosition(position);
		mMap.animateCamera(update);
//		LatLng nvaImperial = new LatLng(-38.745503, -72.948228);
//		CameraPosition position = new CameraPosition(nvaImperial, 10, 70, 1);
//		CameraUpdate update = CameraUpdateFactory.newCameraPosition(position);
//		mMap.animateCamera(update);
//		mMap.addMarker(new MarkerOptions().position(new LatLng(-38.745503, -72.948228)).title("Marker"));
	}

	@Override
	public void onResume() {
		super.onResume();
		mMapView.onResume();
	}

	@Override
	public void onPause() {
		super.onPause();
		mMapView.onPause();
	}

	@Override
	public void onDestroy() {
		mMapView.onDestroy();
		super.onDestroy();
	}
}